﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using HA.Data;

namespace HA.ModFlow.Application
{
   public class MapDataViewModel:MDTVIViewModel
    {
       public MapDataViewModel(ProjectDataViewModel parent,MapDataCollecion mapData)
           : base(parent,false)
       {
           this._mapData = mapData;


           this._contextMenu = new System.Windows.Controls.ContextMenu();


           //MenuItem mi = new MenuItem();

           //mi.Header = "删除";
          
           //this._contextMenu.Items.Add(mi);

           MenuItem mi = new MenuItem();
           mi.Header = "新建概念模型...";
           mi.Command = DataCommands.AddConceptualModel;
           this._contextMenu.Items.Add(mi);

           //mi = new MenuItem();
           //mi.Header = "新建 Converage...";
           //mi.Command = DataCommands.AddCoverage;
          

           //this._contextMenu.Items.Add(mi);

           this._imageSource = VMImageSource.MapData;



           this.LoadChildren();
 
       }

       public override string Desc
       {
           get
           {
               return "映射数据";
           }
       }

       MapDataCollecion _mapData;

       protected override void LoadChildren()
       {

           //foreach (CoverageBase item in this._mapData.Coverages)
           //{

           //    if (item is Coverage)
           //    {
           //        CoverageViewModel cvm = new CoverageViewModel(this,(Coverage)item);

           //        this.Children.Add(cvm);

           //        continue;
                   
           //    }

           //    if (item is ConceptualModel)
           //    {
           //        ConceptualModelViewModel cmvm = new ConceptualModelViewModel(this,(ConceptualModel)item);

           //        this.Children.Add(cmvm);
           //    }
               
           //}

           if (this._mapData.GridFrame!=null)
           {

               GridFrameViewModel gfvm = new GridFrameViewModel(this);

               this.Children.Add(gfvm);


               
           }



           foreach (ConceptualModel cm in this._mapData.ConceptualModelCL)
           {

               ConceptualModelViewModel cmvm = new ConceptualModelViewModel(this, cm);

                this.Children.Add(cmvm);

           }


           this._mapData.ConceputalModelCollectionChanged += new EventHandler<CollecionChangedEventArgs>(_mapData_CollectionChanged);

           this._mapData.GridFrameChanged += new EventHandler<ObjectOperationEventArgs>(_mapData_GridFrameChanged);
       }

       void _mapData_GridFrameChanged(object sender, ObjectOperationEventArgs e)
       {
           switch (e.OperationType)
           {
               case ObjectOperationType.ADD:

                   //GridFrameViewModel gvOld = this.GetGridFrameViewMode();
                   //if (gvOld == null)
                   //{
                       GridFrameViewModel gfvm = new GridFrameViewModel(this);

                       this.Children.Add(gfvm);

                       gfvm.IsSelected = true;
                   //}
                   //else
                   //{
 
                   //}
                   




                   break;
               case ObjectOperationType.REMOVE:

                   GridFrameViewModel gv = this.GetGridFrameViewMode();
                   if (gv!=null)
                   {
                       this.Children.Remove(gv);
                       
                   }

                   break;
               case ObjectOperationType.MODIFY:

                   GridFrameViewModel gvM = this.GetGridFrameViewMode();

                   if (gvM!=null)
                   {
                    
                       
                   }


                   break;
               default:
                   break;
           }


           ProjectDataViewModel parent = (ProjectDataViewModel)this.Parent;

           parent.CollectionView.Refresh();
           
       }

       void _mapData_CollectionChanged(object sender, CollecionChangedEventArgs e)
       {
           switch (e.Type)
           {
               case CollectionChangedType.ADD:


                 
                     ConceptualModelViewModel cmvm = new ConceptualModelViewModel(this,(ConceptualModel)e.Child);

                     this.Children.Add(cmvm);
                       
               

                   break;
               case CollectionChangedType.REMOVE:

                    ConceptualModel cm=(ConceptualModel)e.Child;

                    ConceptualModelViewModel vm = this.GetConceptualModelViewModel(cm);
                    if (vm!=null)
                    {
                        this.Children.Remove(vm);
                        
                    }

                   


                   break;
               case CollectionChangedType.MOVE:
                   break;
               case CollectionChangedType.REPLACE:
                   break;
               case CollectionChangedType.RESET:
                   break;
               default:
                   break;
           }

           ProjectDataViewModel parent = (ProjectDataViewModel)this.Parent;

           parent.CollectionView.Refresh();

       }


       protected ConceptualModelViewModel GetConceptualModelViewModel(ConceptualModel cm)
       {
           foreach (MDTVIViewModel item in this.Children)
           {
               if (item is ConceptualModelViewModel)
               {
                   ConceptualModelViewModel cvm=item as ConceptualModelViewModel;

                   if (cvm.ConceptualModel == cm)
                   {

                       return cvm;


                   }

               }

            


           }

           return null;
           
       }


       protected GridFrameViewModel GetGridFrameViewMode()
       {

           foreach (MDTVIViewModel item in this.Children)
           {
               if (item is GridFrameViewModel)
               {

                   return (GridFrameViewModel)item;

               }




           }

           return null;
 
       }


       protected override void OnSelectedChanged()
       {

           if (this.IsSelected)
           {

               UIModule.DefaultModule.RibbonV2ViewModel.ViewOpViewModel.ToolVM.CurrentTool = APPTOOLMODE.MAPDATA;
               
           }

           
       }

    }
}
